package com.gdcp.service.impl;

import com.gdcp.beans.Cart;
import com.gdcp.beans.Product;
import com.gdcp.common.ResponseModel;
import com.gdcp.mapper.CartMapper;
import com.gdcp.service.CartService;
import com.gdcp.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author 杨华京
 * @version 1.0
 * @description
 * @since 1.8
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    ResponseModel responseModel;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductService productService;

    @Override
    public Cart getOne(Integer uid, Integer pid) {
        Cart cart=cartMapper.getOne(uid,pid);
        return cart;
    }

    @Override
    public Map<String, Object> update(Cart cartItem) {
        try{
            cartMapper.update(cartItem);
            return responseModel.setModel(true,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return responseModel.setModel(false,"修改失败");
        }
    }

    @Override
    public Map<String, Object> add(Cart cartItem) {
        try{
            cartMapper.add(cartItem);
            return responseModel.setModel(true,"添入成功");
        }catch (Exception e){
            e.printStackTrace();
            return responseModel.setModel(false,"添入失败");
        }
    }

    @Override
    public List<Cart> queryByUid(Integer uid) {
        List<Cart> carts=cartMapper.queryByUid(uid);
        for (Cart cart:carts){
            Product product = productService.productInfoById(cart.getProduct_id());
            cart.setProduct(product);
        }
        return carts;
    }

    @Override
    public Map<String, Object> deleteCartById(Integer id) {

        try{
            cartMapper.deleteCartById(id);
            return responseModel.setModel(true,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return responseModel.setModel(false,"删除失败");
        }
    }

    @Override
    public Cart queryByid(Integer id) {
        Cart mycart = new Cart();
        List<Cart> carts=cartMapper.queryById(id);
        for (Cart cart:carts){
            mycart=cart;
        }
        return mycart;
    }
}
